package br.com.klis.batendoumabola.client;

import br.com.klis.batendoumabola.client.service.LoginService;
import br.com.klis.batendoumabola.client.service.LoginServiceAsync;
import br.com.klis.batendoumabola.client.service.PeladaService;
import br.com.klis.batendoumabola.client.service.PeladaServiceAsync;
import br.com.klis.batendoumabola.client.util.Helper;
import br.com.klis.batendoumabola.shared.LoginInfo;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.maps.client.Maps;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class BatendoUmaBola implements EntryPoint {
	LoginInfo loginInfo = null;

	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	/*
	 * private static final String SERVER_ERROR = "An error occurred while " +
	 * "attempting to contact the server. Please check your network " +
	 * "connection and try again.";
	 */
	/**
	 * Create a remote service proxy to talk to the server-side Greeting
	 * service.
	 */
	/*
	 * private final GreetingServiceAsync greetingService = GWT
	 * .create(GreetingService.class);
	 */

	/**
	 * This is the entry point method. Asynchronously loads the Maps API.
	 * 
	 * The first parameter should be a valid Maps API Key to deploy this
	 * application on a public server, but a blank key will work for an
	 * application served from localhost.
	 */
	public void onModuleLoad() {
		/*//Maps.loadMapsApi("ABQIAAAA6J2V73gSoRvCQ7HAPn8PXBSeuyKXrI-W5IqV1vcPVvO6FJbFcxSMkB94zLuoF1cwok8r9ieuSzOktQ", "2", false, new Runnable() {
		Maps.loadMapsApi("ABQIAAAA6J2V73gSoRvCQ7HAPn8PXBR49yX15zcRYVxfp2IYU4o_RTeSWhT2A11YcsR4xI1WLos3d2ixmhmeaw", "2", false, new Runnable() {
		
			//Maps.loadMapsApi("", "2", false, new Runnable() {
			public void run() {
				buildUi();
			}
		});*/
		buildUi();
	}

	private void buildUi() {
		LoginServiceAsync loginService = GWT.create(LoginService.class);
		loginService.login(GWT.getHostPageBaseURL(),
				new AsyncCallback<LoginInfo>() {
					public void onFailure(Throwable error) {
						Helper.handleError(error,loginInfo);
					}

					public void onSuccess(LoginInfo result) {
						loginInfo = result;
						init();
					}
				});
	}

	private void init() {
		GWT.log("entrou no init...");
		PeladaServiceAsync rpcService = GWT.create(PeladaService.class);
		HandlerManager eventBus = new HandlerManager(null);
		AppController appViewer = new AppController(rpcService, eventBus, loginInfo);
		RootPanel.get("loading").setVisible(false); 
		appViewer.go(RootPanel.get());
	}

	/*private void handleError(Throwable error) {
		Window.alert(error.getMessage());
		if (error instanceof NotLoggedInException) {
			Window.Location.replace(loginInfo.getLogoutUrl());
		}
	}*/
}
